import axios from "axios";

const service = axios.create({
  // process.env.VUE_APP_BASE_API
  baseURL: process.env.VUE_APP_BASE_API,
  // withCredentials: true, // 当跨域发送请求时
  timeout: 5000,
})

// request interceptor
service.interceptors.request.use(
  config => {
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

service.interceptors.response.use(
  response => {
    const res = response.data
    if (res.code !== 200) {

      if(res.code === 500){
        // 服务器错误
      } else if(res.code === 400 ||res.code === 401 || res.code === 404){
        // 请求页面错误
      }
      
      return Promise.reject(new Error(res.message || 'Error'))
    } else {
      return res
    }
  },
  error => {
    const res = error.response;
    console.error(res);
    return Promise.reject(error)
  }
)

export default service
